define(function() {

    var isPreview = false;

    function init(pv) {
        isPreview = pv || false;
        $('.Js-curActive').find('.mobile-comppont').each(function() {
            playAni($(this));
        });
    }

    function playAni($ele) {
        var aniSq = $ele.data('info').ani;
        if (aniSq.length == 0) {
            return;
        }
        $ele.css('animation', '');

        var i = 0;
        if (!isPreview) {
            aniSq[i].anifimd = 'both';
        }
        setTimeout(function() {
            $ele.css({
                'animation': '',
                'opacity': 1,
                'animation': aniSq[i].aniname + ' ' + aniSq[i].durtime + 's ' + aniSq[i].anitimfn + ' ' + aniSq[i].delaytime + 's ' + aniSq[i].anifimd + ' ' + aniSq[i].lptimes
            });
        }, 0);
        $ele.on("animationend", function() { //动画结束时事件
            i++;
            if (i >= aniSq.length) {
                $ele.off("animationend");
                return;
            }
            if (!isPreview) {
                aniSq[i].anifimd = 'both';
            }
            $ele.css('animation', '');
            setTimeout(function() {
                $ele.css('animation', aniSq[i].aniname + ' ' + aniSq[i].durtime + 's ' + aniSq[i].anitimfn + ' ' + aniSq[i].delaytime + 's ' + aniSq[i].anifimd + ' ' + aniSq[i].lptimes);
            }, 0);
        });
    }

    function clearAni(ele) {
        ele.find('.mobile-comppont').each(function() {
            $(this).css({
                'opacity': 0,
                'animation': ''
            });
        });
    }

    var swipe = {
        _events: {}, // 自定义事件---this._execEvent('scrollStart');
        _page: $('#Js-container li'), // 模版页面切换的页面集合
        _pageNum: $('#Js-container li').size(), // 模版页面的个数
        _pageNow: 0, // 页面当前的index数
        _pageNext: null, // 页面下一个的index数

        _touchStartValY: 0, // 触摸开始获取的第一个值
        _touchDeltaY: 0, // 滑动的距离

        _moveStart: true, // 触摸移动是否开始
        _movePosition: null, // 触摸移动的方向（上、下）
        _movePosition_c: null, // 触摸移动的方向的控制
        _mouseDown: false, // 判断鼠标是否按下
        _moveFirst: true,
        _moveInit: false,
        _pageNextNode: null, //下个页面node

        _translateZ: function() {
            return ' translateZ(0)';
        },
        // 自定义事件操作
        _handleEvent: function(type) {
            if (!this._events[type]) {
                return;
            }

            var i = 0,
                l = this._events[type].length;

            if (!l) {
                return;
            }

            for (; i < l; i++) {
                this._events[type][i].apply(this, [].slice.call(arguments, 1));
            }
        },
        // 给自定义事件绑定函数
        _on: function(type, fn) {
            if (!this._events[type]) {
                this._events[type] = [];
            }

            this._events[type].push(fn);
        },
        // 页面切换开始
        page_start: function() {
            swipe._page.on('touchstart mousedown', swipe.page_touch_start);
            swipe._page.on('touchmove mousemove', swipe.page_touch_move);
            swipe._page.on('touchend mouseup', swipe.page_touch_end);
        },
        // 页面切换停止
        page_stop: function() {
            swipe._page.off('touchstart mousedown');
            swipe._page.off('touchmove mousemove');
            swipe._page.off('touchend mouseup');
        },
        // page触摸移动start
        page_touch_start: function(e) {

            if (!swipe._moveStart) return;
            if (e.type == "touchstart") {
                swipe._touchStartValY = window.event.touches[0].pageY;
            } else {
                swipe._touchStartValY = e.pageY || e.y;
                swipe._mouseDown = true;
            }

            swipe._moveInit = true;

            // start事件
            swipe._handleEvent('start');
        },
        // page触摸移动move
        page_touch_move: function(e) {
            e.preventDefault();
            if (!swipe._moveStart) return;
            if (!swipe._moveInit) return;
            // 设置变量值
            var $self = swipe._page.eq(swipe._pageNow),
                h = parseInt($self.height()),
                moveP,
                scrollTop,
                node = null,
                move = false;
            // 获取移动的值
            if (e.type == "touchmove") {
                moveP = window.event.touches[0].pageY;
                move = true;
            } else {
                if (swipe._mouseDown) {
                    moveP = e.pageY || e.y;
                    move = true;
                } else return;
            }
            // 获取下次活动的page
            node = swipe.page_position(e, moveP, $self);
            // page页面移动
            swipe.page_translate(node);
            // move事件
            swipe._handleEvent('move');
        },

        // page触摸移动判断方向
        page_position: function(e, moveP, $self) {
            var now, next;

            // 设置移动的距离
            if (moveP != 'undefined') swipe._touchDeltaY = moveP - swipe._touchStartValY;

            // 设置移动方向
            swipe._movePosition = moveP - swipe._touchStartValY > 0 ? 'down' : 'up';
            if (swipe._movePosition != swipe._movePosition_c) {
                swipe._moveFirst = true;
                swipe._movePosition_c = swipe._movePosition;
            } else {
                swipe._moveFirst = false;
            }

            // 设置下一页面的显示和位置
            if (swipe._touchDeltaY <= 0) {
                if ($self.next('#Js-container li').length == 0) {
                    swipe._pageNext = 0;
                } else {
                    swipe._pageNext = swipe._pageNow + 1;
                }

                next = swipe._page.eq(swipe._pageNext)[0];
            } else {
                if ($self.prev('#Js-container li').length == 0) {
                    if (swipe._firstChange) {
                        swipe._pageNext = swipe._pageNum - 1;
                    } else {
                        return;
                    }
                } else {
                    swipe._pageNext = swipe._pageNow - 1;
                }

                next = swipe._page.eq(swipe._pageNext)[0];
            }
            now = swipe._page.eq(swipe._pageNow)[0];
            node = [next, now];

            // move阶段根据方向设置页面的初始化位置--执行一次
            if (swipe._moveFirst) init_next(node);

            function init_next(node) {
                var s, l, _translateZ = swipe._translateZ();

                swipe._page.removeClass('action');
                $(node[1]).addClass('action').removeClass('f-hide');
                swipe._page.not('.action').addClass('f-hide');

                // 模版高度适配函数处理
                //swipe.height_auto(swipe._page.eq(swipe._pageNext),'false');

                // 显示对应移动的page
                $(node[0]).removeClass('f-hide').addClass('active');
                swipe._pageNextNode = $(node[0]);
                clearAni($(node[0]));

                // 设置下一页面的显示和位置
                if (swipe._movePosition == 'up') {
                    s = parseInt($(window).scrollTop());
                    if (s > 0) l = $(window).height() + s;
                    else l = $(window).height();
                    node[0].style['transform'] = 'translate(0,' + l + 'px)' + _translateZ;
                    $(node[0]).attr('data-translate', l);
                    $(node[1]).attr('data-translate', 0);
                } else {
                    node[0].style['transform'] = 'translate(0,-' + Math.max($(window).height(), $(node[0]).height()) + 'px)' + _translateZ;
                    $(node[0]).attr('data-translate', -Math.max($(window).height(), $(node[0]).height()));
                    $(node[1]).attr('data-translate', 0);
                }
            }
            return node;
        },

        // page触摸移动设置函数
        page_translate: function(node) {
            // 没有传值返回
            if (!node) return;
            var _translateZ = swipe._translateZ(),
                y_1, y_2, scale,
                y = swipe._touchDeltaY * 1.2;

            // 切换的页面移动
            if ($(node[0]).attr('data-translate')) y_1 = y + parseInt($(node[0]).attr('data-translate'));
            node[0].style['transform'] = 'translate(0,' + (y_1 - 80) + 'px)' + _translateZ;
            // 当前的页面移动
            if ($(node[1]).attr('data-translate')) y_2 = y + parseInt($(node[1]).attr('data-translate'));
            scale = 1 - Math.abs(y * 0.6 / $(window).height());
            y_2 = y_2 / 5;
            node[1].style['transform'] = 'translate(0,' + y_2 + 'px)' + _translateZ + ' scale(' + scale + ')';

        },

        // page触摸移动end
        page_touch_end: function(e) {
            swipe._moveInit = false;
            swipe._mouseDown = false;
            if (!swipe._moveStart) return;
            if (!swipe._pageNext && swipe._pageNext != 0) return;

            swipe._moveStart = false;

            // 确保移动了
            if (Math.abs(swipe._touchDeltaY) > 10) {
                swipe._page.eq(swipe._pageNext)[0].style['transition'] = 'all .3s';
                swipe._page.eq(swipe._pageNow)[0].style['transition'] = 'all .3s';
            }
            // 页面切换
            if (Math.abs(swipe._touchDeltaY) >= 100) { // 切换成功
                swipe.page_success();
            } else if (Math.abs(swipe._touchDeltaY) > 10 && Math.abs(swipe._touchDeltaY) < 100) { // 切换失败
                swipe.page_fial();
            } else { // 没有切换
                swipe.page_fial();
            }

            // end事件
            swipe._handleEvent('end');

            // 注销控制值
            swipe._movePosition = null;
            swipe._movePosition_c = null;
            swipe._touchStartValY = 0;

            if ($('#j-mengban').hasClass('z-show') && swipe._pageNext == mengvalue) {
                swipe.page_stop();
            }
        },

        // 切换成功
        page_success: function() {
            var _translateZ = swipe._translateZ();
            // 下一个页面的移动
            swipe._page.eq(swipe._pageNext)[0].style['transform'] = 'translate(0,0)' + _translateZ;
            // 当前页面变小的移动
            var y = swipe._touchDeltaY > 0 ? $(window).height() / 5 : -$(window).height() / 5;
            //var scale = 0.6;
            var scale = 1 - Math.abs(y * 0.6 / $(window).height());
            swipe._page.eq(swipe._pageNow)[0].style['transform'] = 'translate(0,' + y + 'px)' + _translateZ + ' scale(' + scale + ')';
            // 成功事件
            swipe._handleEvent('success');
        },

        // 切换失败
        page_fial: function() {
            var _translateZ = swipe._translateZ();
            // 判断是否移动了
            if (!swipe._pageNext && swipe._pageNext != 0) {
                swipe._moveStart = true;
                swipe._moveFirst = true;
                return;
            }

            if (swipe._movePosition == 'up') {
                swipe._page.eq(swipe._pageNext)[0].style['transform'] = 'translate(0,' + $(window).height() + 'px)' + _translateZ;
            } else {
                swipe._page.eq(swipe._pageNext)[0].style['transform'] = 'translate(0,-' + $(window).height() + 'px)' + _translateZ;
            }
            swipe._page.eq(swipe._pageNow)[0].style['transform'] = 'translate(0,0)' + _translateZ + ' scale(1)';
            // fial事件
            swipe._handleEvent('fial');
        },

        haddle_envent_fn: function() {
            // 当前页面移动，延迟加载以后的图片
            //swipe._on('start',swipe.lazy_bigP);
            // 当前页面移动
            // swipe._on('move',function(){
            // });

            // 切换失败事件
            swipe._on('fial', function() {
                setTimeout(function() {
                    swipe._page.eq(swipe._pageNow).attr('data-translate', '');
                    swipe._page.eq(swipe._pageNow)[0].style['transform'] = '';
                    swipe._page.eq(swipe._pageNow)[0].style['transition'] = '';
                    swipe._page.eq(swipe._pageNext)[0].style['transform'] = '';
                    swipe._page.eq(swipe._pageNext)[0].style['transition'] = '';

                    swipe._page.eq(swipe._pageNext).removeClass('active').addClass('f-hide');
                    swipe._moveStart = true;
                    swipe._moveFirst = true;
                    swipe._pageNext = null;
                    swipe._touchDeltaY = 0;
                    //swipe._page.eq(swipe._pageNow).attr('style','');
                    swipe._page.eq(swipe._pageNow)[0].style['transform'] = '';
                }, 300)
            });

            // 切换成功事件
            swipe._on('success', function() {
                // 判断最后一页让，开启循环切换
                if (swipe._pageNext == 0 && swipe._pageNow == swipe._pageNum - 1) {
                    swipe._firstChange = true;
                    //window.location.href="http://www.5.cn/magazine/822/1883/index.html";
                }
                // 判断是否是最后一页，让轻APP关联页面隐藏
                //if(swipe._page.eq(swipe._pageNext).next('#Js-container li').length != 0){
                //swipe.lightapp_intro_hide(true);
                //}
                setTimeout(function() {
                    // 判断是否为最后一页，显示或者隐藏箭头
                    //if(swipe._pageNext == swipe._pageNum-1 ) $('.u-arrow').addClass('f-hide');
                    //else  $('.u-arrow').removeClass('f-hide');

                    swipe._page.eq(swipe._pageNow).addClass('f-hide');

                    swipe._page.eq(swipe._pageNow).attr('data-translate', '');
                    swipe._page.eq(swipe._pageNow)[0].style['transform'] = '';
                    swipe._page.eq(swipe._pageNow)[0].style['transition'] = '';
                    swipe._page.eq(swipe._pageNext)[0].style['transform'] = '';
                    swipe._page.eq(swipe._pageNext)[0].style['transition'] = '';

                    // 初始化切换的相关控制值
                    //$('.p-ct').removeClass('fixed');
                    swipe._page.eq(swipe._pageNext).removeClass('active');
                    //swipe._page.eq(swipe._pageNext).removeClass('fixed');
                    swipe._pageNow = swipe._pageNext;
                    swipe._moveStart = true;
                    swipe._moveFirst = true;
                    swipe._pageNext = null;
                    //swipe._page.eq(swipe._pageNow).attr('style','');
                    swipe._page.eq(swipe._pageNow)[0].style['transform'] = '';
                    //swipe._page.eq(swipe._pageNow).removeClass('fixed');
                    swipe._page.eq(swipe._pageNow).attr('data-translate', '');
                    swipe._touchDeltaY = 0;
                    // 切换成功后，执行当前页面的动画---延迟200ms
                    setTimeout(function() {
                        //if(swipe._page.eq(swipe._pageNow).hasClass('z-animate')) return;
                        //swipe._page.eq(swipe._pageNow).addClass('z-animate');
                        init(false, swipe._pageNextNode);
                    }, 20);

                    /*// 隐藏图文组件的文本
					$('.j-detail').removeClass('z-show');
					$('.txt-arrow').removeClass('z-toggle');

					// 切换停止视频的播放
					$('video').each(function(){
						if(!this.paused) this.pause();
					})

					// 判断是否滑动最后一页，并让轻APP介绍关联页面贤淑
		 			if(swipe._page.eq(swipe._pageNow).next().next('#Js-container li').length == 0){
		 				swipe.lightapp_intro_show();
		 				swipe.lightapp_intro();
						$(".market-notice").show();
		 			}
					if(swipe._page.eq(swipe._pageNow).next('#Js-container li').length == 0){
		 				swipe.lightapp_intro_hide(false);
		 				$(".market-notice").hide();
		 			}*/
                }, 300)
            })
        },
        init: function() {
            this.haddle_envent_fn();
            swipe.page_start();
        }
    };
    swipe.init();

    return init;
});
